const LogService = require('../services/log.service')
const RegistrationService = require('../services/registration.service')

const registrationService = new RegistrationService()
const logService = new LogService()

class ReportController {
  async findAll (ctx, next) {
    const query = ctx.request.query
    if (query.reg_dwmc || query.reg_xzxq) {
      const registrations = await registrationService.findAll({
        reg_xzxq: query.reg_xzxq,
        pageSize: 999999
      })
      const placeIds = registrations.data.map(o => o.place._id)
      query.ref = { $in: placeIds }
    }
    const data = await logService.findAll(query)
    ctx.response.body = data
  }
  async findOne (ctx, next) {
    const id = ctx.request.params.id
    const data = await logService.findOne(id)
    ctx.response.body = data[0]
  }
  async updateOne (ctx, next) {
    const id = ctx.request.params.id
    const body = ctx.request.body
    const data = await logService.updateOne(id, body)
    ctx.response.body = data
  }
  async deleteOne (ctx, next) {
    const id = ctx.request.params.id
    const data = await logService.deleteOne(id)
    ctx.response.body = data
  }
}

module.exports = ReportController